package com.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usercenter.entity.AdminProfileExtendEntity;
import com.usercenter.mapper.AdminProfileMapper;
import com.usercenter.service.AdminProfileService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 管理员个人资料服务实现
 */
@Service
public class AdminProfileServiceImpl extends ServiceImpl<AdminProfileMapper, AdminProfileExtendEntity> implements AdminProfileService {

    @Override
    public AdminProfileExtendEntity getProfileByAccount(String account) {
        LambdaQueryWrapper<AdminProfileExtendEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminProfileExtendEntity::getAccount, account);
        return this.getOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProfile(AdminProfileExtendEntity profile) {
        profile.setUpdateTime(new Date());
        return this.updateById(profile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean initProfile(String account) {
        // 检查是否已存在
        if (getProfileByAccount(account) != null) {
            return true;
        }
        
        // 创建新的个人资料
        AdminProfileExtendEntity profile = new AdminProfileExtendEntity();
        profile.setAccount(account);
        profile.setGender(0);  // 默认未知
        profile.setSignature("系统管理员");
        profile.setUpdateTime(new Date());
        
        return this.save(profile);
    }
    
    @Override
    public Object getDepartmentList() {
        List<Map<String, Object>> departments = new ArrayList<>();
        
        Map<String, Object> dept1 = new HashMap<>();
        dept1.put("id", "IT_DEPT");
        dept1.put("name", "信息技术部");
        
        Map<String, Object> dept2 = new HashMap<>();
        dept2.put("id", "OP_DEPT");
        dept2.put("name", "运营部");
        
        Map<String, Object> dept3 = new HashMap<>();
        dept3.put("id", "FINANCE_DEPT");
        dept3.put("name", "财务部");
        
        Map<String, Object> dept4 = new HashMap<>();
        dept4.put("id", "ADMIN_DEPT");
        dept4.put("name", "行政部");
        
        departments.add(dept1);
        departments.add(dept2);
        departments.add(dept3);
        departments.add(dept4);
        
        return departments;
    }
} 